﻿using Cognex.VisionPro;
using Cognex.VisionPro.Implementation.Internal;
using Cognex.VisionPro.PMAlign;

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace 卡尺寻址
{
    public class Class1
    {
        /// <summary>
        /// 获取PMA工具建模器数据
        /// </summary>
        /// <param name="CogPMATool"> PMA工具 </param>
        /// <param name="result">     建模器数据结果 </param>
        /// 返回数据结果格式：A,StartX,StartY,EndX,EndY,Rotation
        public static void GetPMATrainData(CogPMAlignEditV2 CogPMATool, out List<string> result)
        {
            CogShapeModelPropCollection mCogShpeModelCollection = new CogShapeModelPropCollection();
            result = new List<string>();
            mCogShpeModelCollection = (CogShapeModelPropCollection)CogPMATool.Subject.Pattern.TrainShapeModels;
            int ShpeModeCount = mCogShpeModelCollection.Count;
            CogGeneralContour mCogGeneralContour = new CogGeneralContour();

            try
            {
                for (int i = 0; i < ShpeModeCount; i++)
                {
                    mCogGeneralContour = mCogShpeModelCollection[i].Shape as CogGeneralContour;
                    for (int j = 0; j < mCogGeneralContour.SegmentCount; j++)
                    {
                        result.Add("A");
                        result.Add(mCogGeneralContour.CreateSegmentIterator(j).StartX.ToString("F3"));
                        result.Add(mCogGeneralContour.CreateSegmentIterator(j).StartY.ToString("F3"));
                        result.Add(mCogGeneralContour.CreateSegmentIterator(j).EndX.ToString("F3"));
                        result.Add(mCogGeneralContour.CreateSegmentIterator(j).EndY.ToString("F3"));
                        result.Add(mCogGeneralContour.CreateSegmentIterator(j).StartAngle.ToString("F3"));
                        SaveRunParams("建模器数据", result);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return;
            }
        }

        public static void GetTag(System.Windows.Forms.Control cons)
        {
            try
            {
                foreach (Control con in cons.Controls)
                {
                    con.Tag = con.Width + ";" + con.Height + ";" + con.Top + ";" + con.Left + ";" + con.Font.Size;
                    if (cons.Controls.Count > 0)
                    {
                        GetTag(con);
                    }
                }
            }
            catch
            {
            }
        }

        public static object LoadVpp(string name)
        {
            return CogSerializer.LoadObjectFromFile(System.Windows.Forms.Application.StartupPath + "\\RunParams\\" + name + ".vpp");
        }

        public static void SaveRunParams(string _filepath, List<string> _list)
        {
            if (!System.IO.Directory.Exists(System.Windows.Forms.Application.StartupPath + "\\RunParams"))
            {
                System.IO.Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + "\\RunParams");
            }
            string path = System.Windows.Forms.Application.StartupPath + "\\RunParams\\" + _filepath + ".txt";
            using (StreamWriter streamWriter = new StreamWriter(path, true, Encoding.Default))
            {
                foreach (var item in _list)
                {
                    streamWriter.Write(item + ";");
                }
                streamWriter.Flush();
                streamWriter.Close();
            }
            GC.Collect();
        }

        public static void SaveVpp(string name, string path)
        {
            CogSerializer.SaveObjectToFile(name, path, typeof(System.Runtime.Serialization.Formatter), CogSerializationOptionsConstants.Minimum);
        }

        public static void SetControl(System.Windows.Forms.Control cons, float newx, float newy)
        {
            try
            {
                foreach (System.Windows.Forms.Control con in cons.Controls)
                {
                    if (con.Tag == null)
                    {
                        continue;
                    }
                    string[] myTag = con.Top.ToString().Split(';');
                    con.Width = Convert.ToInt32(Convert.ToSingle(myTag[0]) * newx);
                    con.Height = Convert.ToInt32(Convert.ToSingle(myTag[1]) * newy);
                    con.Top = Convert.ToInt32(Convert.ToSingle(myTag[2]) * newx);
                    con.Left = Convert.ToInt32(Convert.ToSingle(myTag[3]) * newy);
                    con.Font = new System.Drawing.Font(con.Font.Name, Convert.ToSingle(myTag[4]), con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        SetControl(con, newx, newy);
                    }
                }
            }
            catch
            {
            }
        }
    }
}